<!---
*******************************************************************************
Copyright (C) 2006 Ryan Hickman (rhickman@gmail.com)
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
     http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*******************************************************************************

Description:
This ColdFusion custom tag will accept any numeric value and pass back the 
expanded text using any variable name specified.

Usage:
	<cf_number2text
		NumberString="123456"
		NewTextString="Dollars_Text">
		
*******************************************************************************
--->

<!--- Set Temp Variables --->
<cfparam name="Attributes.NumberString" default="0">
<cfparam name="Attributes.NewTextString" default="Dollars_Text">
<cfset OutputVar="Caller." & Attributes.NewTextString>
<cfset TempNumString=Int(Attributes.NumberString)>
<cfset LoopCount=Ceiling(Len(TempNumString) / 3)>

<!--- Built The New Text String --->
<cfif TempNumString EQ 0>
	<cfset TempTextString="Zero">
<cfelse>
	<cfset TempTextString="">
	
	<!--- Loop by the thousands --->
	<cfloop from="1" to="#LoopCount#" index="counter">
		
		<!--- Reset these values on each loop pass --->
		<cfset TempNum=TempNumString>
		<cfset TempText="">
		
		<!--- Handle the hundred's --->
		<cfset TempNum=TempNum - (Int(TempNum / 1000) * 1000)>
		<cfif TempNum GT 99>
			<cfswitch expression=#Int(TempNum /100)#>
			  <cfcase value = "1">
			  	<cfset TempText=TempText & "One">
			  </cfcase>
			  <cfcase value = "2">
			 	<cfset TempText=TempText & "Two">
			  </cfcase>
			  <cfcase value = "3">
			  	<cfset TempText=TempText & "Three">
			  </cfcase>
			  <cfcase value = "4">
			  	<cfset TempText=TempText & "Four">
			  </cfcase>
			  <cfcase value = "5">
			  	<cfset TempText=TempText & "Five">
			  </cfcase>
			  <cfcase value = "6">
			  	<cfset TempText=TempText & "Six">
			  </cfcase>
			  <cfcase value = "7">
			  	<cfset TempText=TempText & "Seven">
			  </cfcase>
			  <cfcase value = "8">
			  	<cfset TempText=TempText & "Eight">
			  </cfcase>
			  <cfcase value = "9">
			  	<cfset TempText=TempText & "Nine">
			  </cfcase>
			</cfswitch>
			<cfif TempNum GT 0>
				<cfset TempText=TempText & " Hundred ">
			</cfif>
		</cfif>
		
		<!--- Handle the tens --->
		<cfset TempNum=TempNum - (Int(TempNum / 100) * 100)>
		<cfif TempNum GT 19 OR TempNum LT 10>	
			<cfswitch expression = #Int(TempNum /10)#>
				<cfcase value = "1">
					<cfset TempText=TempText & "Ten">
				</cfcase>
				<cfcase value = "2">
					<cfset TempText=TempText & "Twenty">
				</cfcase>
				<cfcase value = "3">
					<cfset TempText=TempText & "Thirty">
				</cfcase>
				<cfcase value = "4">
					<cfset TempText=TempText & "Fourty">
				</cfcase>
				<cfcase value = "5">
					<cfset TempText=TempText & "Fifty">
				</cfcase>
				<cfcase value = "6">
					<cfset TempText=TempText & "Sixty">
				</cfcase>
				<cfcase value = "7">
					<cfset TempText=TempText & "Seventy">
				</cfcase>
				<cfcase value = "8">
					<cfset TempText=TempText & "Eighty">
				</cfcase>
				<cfcase value = "9">
					<cfset TempText=TempText & "Ninety">
				</cfcase>
			</cfswitch>
	
			<!--- Handle the one's --->
			<cfset TempNum=TempNum - (Int(TempNum / 10) * 10)>
			<cfswitch expression=#TempNum#>
			  <cfcase value = "1">
			  	<cfset TempText=TempText & " One">
			  </cfcase>
			  <cfcase value = "2">
			 	<cfset TempText=TempText & " Two">
			  </cfcase>
			  <cfcase value = "3">
			  	<cfset TempText=TempText & " Three">
			  </cfcase>
			  <cfcase value = "4">
			  	<cfset TempText=TempText & " Four">
			  </cfcase>
			  <cfcase value = "5">
			  	<cfset TempText=TempText & " Five">
			  </cfcase>
			  <cfcase value = "6">
			  	<cfset TempText=TempText & " Six">
			  </cfcase>
			  <cfcase value = "7">
			  	<cfset TempText=TempText & " Seven">
			  </cfcase>
			  <cfcase value = "8">
			  	<cfset TempText=TempText & " Eight">
			  </cfcase>
			  <cfcase value = "9">
			  	<cfset TempText=TempText & " Nine">
			  </cfcase>
			</cfswitch>
		<cfelse>

			<!--- Handle the teens --->
			<cfswitch expression=#TempNum#>
				<cfcase value = "10">
					<cfset TempText=TempText & "Ten">
				</cfcase>
				<cfcase value = "11">
					<cfset TempText=TempText & "Eleven">
				</cfcase>
				<cfcase value = "12">
					<cfset TempText=TempText & "Twelve">
				</cfcase>
				<cfcase value = "13">
					<cfset TempText=TempText & "Thirteen">
				</cfcase>
				<cfcase value = "14">
					<cfset TempText=TempText & "Fourteen">
				</cfcase>
				<cfcase value = "15">
					<cfset TempText=TempText & "Fifteen">
				</cfcase>
				<cfcase value = "16">
					<cfset TempText=TempText & "Sixteen">
				</cfcase>
				<cfcase value = "17">
					<cfset TempText=TempText & "Seventeen">
				</cfcase>
				<cfcase value = "18">
					<cfset TempText=TempText & "Eighteen">
				</cfcase>
				<cfcase value = "19">
					<cfset TempText=TempText & "Nineteen">
				</cfcase>
			</cfswitch>
		</cfif>
		
		<!--- Append thousandths text --->
		<cfswitch expression=#Counter#>
				<cfcase value = "2">
					<cfset TempText=TempText & " Thousand ">
				</cfcase>
				<cfcase value = "3">
					<cfset TempText=TempText & " Million ">
				</cfcase>
				<cfcase value = "4">
					<cfset TempText=TempText & " Billion ">
				</cfcase>
				<cfcase value = "5">
					<cfset TempText=TempText & " Trillion ">
				</cfcase>
			</cfswitch>
			
		<!--- Update these variables at the end of each loop --->
		<cfset TempNumString=Int(TempNumString / 1000)>
		<cfset TempTextString=TempText & TempTextString>
	</cfloop>
</cfif>

<!--- Pass the New Text String back to the calling page --->
<CFSET temp=SetVariable(OutputVar,TempTextString)>  